/*
 * StrBlobPtr.cpp
 *
 *  Created on: 2020年11月27日
 *      Author: san
 */

#include "StrBlobPtr.h"
#include "StrBlob.h"


// 拷贝构造函数
StrBlobPtr::StrBlobPtr(const StrBlobPtr &a): wptr(a.wptr)
{
	// 拷贝构造函数
}

bool operator ==(const StrBlobPtr &sbp1, const StrBlobPtr &sbp2)
{
	// 如果weak_ptr过期，返回false
	if (sbp1.wptr.expired() || sbp2.wptr.expired())
		return false;
	return *sbp1.wptr.lock() == *sbp2.wptr.lock();
}

StrBlobPtr &StrBlobPtr::operator ++()
{
	// 前置运算符
	check(curr, "increment past end of StrBlobPtr");
	++curr;
	return *this;
}

StrBlobPtr &StrBlobPtr::operator ++(int)
{
	// 后置运算符,只是一个示例，这里不需要
	check(curr, "increment past end of StrBlobPtr");
	curr++;
	return *this;
}

std::string &StrBlobPtr::operator *() const
{
	// 解引用运算符
	auto p = check(curr, "deference past end");
	return (*p)[curr];
}

std::string *StrBlobPtr::operator ->() const
{
	// 将实际工作委托给解引用运算符
	return & this->operator*();
}

std::string &StrBlobPtr::deref() const
{
	// 解引用
	const auto p = check(curr, "dereference past end");
	// 这里可以修改,不好避免
//	(*p)[curr] = "o";
	return (*p)[curr];
}

StrBlobPtr &StrBlobPtr::incr()
{
	// 递增
	check(curr, "increment past end of StrBlobPtr");
	++curr;
	return *this;
}

std::shared_ptr<std::vector<std::string>> StrBlobPtr::check(std::size_t i, const std::string &msg) const
{
	auto ret = wptr.lock();
	if (!ret)  // vector还存在
	{
		throw std::runtime_error("unbound StrBlobPtr");
	}

	if (i > ret->size())
		throw std::out_of_range(msg);
	return ret;
}

StrBlobPtr::~StrBlobPtr() {
	// TODO Auto-generated destructor stub
}

